package routes

import (
	v1 "daily-blog/api/v1"
	"daily-blog/global"
	"daily-blog/middleware"
	"github.com/gin-gonic/gin"
)

func InitRouter() {
	r := gin.New()
	r.Use(gin.Recovery(), middleware.ZapLogger()) // 使用zap作为Gin的日志中间件
	router := r.Group("api/v1")
	// 登录
	router.POST("login", v1.Login)
	// visitor
	initVisitor(router)
	// admin
	admin := router.Group("")
	admin.Use(middleware.AuthMiddleware())
	initUser(admin)
	initPost(admin)
	initTags(admin)
	initCategory(admin)
	initComment(admin)
	// 运行
	server := global.GVA_CONFIG.Server
	r.Run(server.IP + server.HttpPort)
}
func initPost(r *gin.RouterGroup) {
	post := r.Group("post")
	post.POST("", v1.CreatePost)
	post.DELETE("", v1.DeletePost)
	post.PUT("", v1.UpdatePost)
	post.GET("", v1.GetPost)
	post.GET("list", v1.GetPostList)
}
func initTags(r *gin.RouterGroup) {
	tag := r.Group("tag")
	tag.POST("", v1.CreateTag)
	tag.DELETE("", v1.DeleteTag)
	tag.GET("", v1.GetTagList)
}
func initCategory(r *gin.RouterGroup) {
	category := r.Group("category")
	category.POST("", v1.CreateCategory)
	category.DELETE("", v1.DeleteCategory)
	category.PUT("", v1.UpdateCategory)
	category.GET("", v1.GetCategoryList)
}
func initComment(r *gin.RouterGroup) {
	comment := r.Group("comment")
	comment.DELETE("", v1.DeleteComment)
	comment.GET("", v1.GetAllPostCommentList)
}
func initUser(r *gin.RouterGroup) {
	user := r.Group("user")
	user.POST("", v1.CreateUser)

}

// 访客
func initVisitor(r *gin.RouterGroup) {
	visit := r.Group("visit")
	// 文章
	visit.GET("post-list", v1.GetPostList)                      // 所有
	visit.GET("post-with-category", v1.GetPostListWithCategory) // 目录下的所有
	visit.GET("post", v1.GetPost)                               // 获取文章内容
	visit.GET("", v1.GetPostListWithTag)                        // tag相关的所有post
	// 评论
	visit.POST("", v1.CreateComment)        // 创建文章评论
	visit.GET("comment", v1.GetCommentList) // 获取指定文章评论
	// 点赞
	visit.POST("/add-like", v1.AddLike)
	// 目录
	visit.GET("category-list", v1.GetCategoryList) // 所有
}
